﻿/**********************  此代码是由代码生成器生成，生成时间： 2012/12/4 21:29:00  ************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Data.SqlClient;
using System.Data;

using CRM.Entity;
using CRM.DAL.SQLiteHelper;
using System.Data.SQLite;

namespace CRM.DAL
{
    public partial class ArticleInfoDAL
    {
        public bool ArticleInfo_Add(ArticleInfo articleInfo)
        {
            StringBuilder sb = new StringBuilder();

            sb.Append("insert into [ArticleInfo]");
            sb.Append("(");
            sb.Append("Title,");
            sb.Append("ArticleType,");
            sb.Append("Image,");
            sb.Append("ArticleContent,");
            sb.Append("CreateTime,");
            sb.Append("CreateUser,");
            sb.Append("ClickCount,");
            sb.Append("Disable,");
            sb.Append("IsNew,");
            sb.Append("IsTop");
            sb.Append(")");
            sb.Append("values");
            sb.Append("(");
            sb.Append("@Title,");
            sb.Append("@ArticleType,");
            sb.Append("@Image,");
            sb.Append("@ArticleContent,");
            sb.Append("@CreateTime,");
            sb.Append("@CreateUser,");
            sb.Append("@ClickCount,");
            sb.Append("@Disable,");
            sb.Append("@IsNew,");
            sb.Append("@IsTop");
            sb.Append(");select last_insert_rowid()");

            SQLiteParameter[] sqlParameter = new SQLiteParameter[]
            {
                   new SQLiteParameter("@Title",articleInfo.Title),
                   new SQLiteParameter("@ArticleType",articleInfo.ArticleType),
                   new SQLiteParameter("@Image",articleInfo.Image),
                   new SQLiteParameter("@ArticleContent",articleInfo.ArticleContent),
                   new SQLiteParameter("@CreateTime",articleInfo.CreateTime),
                   new SQLiteParameter("@CreateUser",articleInfo.CreateUser),
                   new SQLiteParameter("@ClickCount",articleInfo.ClickCount),
                   new SQLiteParameter("@Disable",articleInfo.Disable),
                   new SQLiteParameter("@IsNew",articleInfo.IsNew),
                   new SQLiteParameter("@IsTop",articleInfo.IsTop),
            };

            try
            {
               var identity = Convert.ToInt32(DbHelperSQL.ExecuteScalar(sb.ToString(), sqlParameter));

                if (identity > 0)
                {
                    articleInfo.ArticleId = identity;
                    return true;
                }
                return false;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                throw;
            }

        }

        public bool ArticleInfo_Update(ArticleInfo articleInfo)
        {
            StringBuilder sb = new StringBuilder();

            sb.Append("update [ArticleInfo] set ");
            sb.Append("Title=@Title,");
            sb.Append("ArticleType=@ArticleType,");
            sb.Append("Image=@Image,");
            sb.Append("ArticleContent=@ArticleContent,");
            sb.Append("CreateTime=@CreateTime,");
            sb.Append("CreateUser=@CreateUser,");
            sb.Append("ClickCount=@ClickCount,");
            sb.Append("Disable=@Disable,");
            sb.Append("IsNew=@IsNew,");
            sb.Append("IsTop=@IsTop");
            sb.Append(" where ArticleId=@ArticleId");
            
            SQLiteParameter[] parameters = 
            {
                  new SQLiteParameter("@ArticleId",articleInfo.ArticleId),
                  new SQLiteParameter("@Title",articleInfo.Title),
                  new SQLiteParameter("@ArticleType",articleInfo.ArticleType),
                  new SQLiteParameter("@Image",articleInfo.Image),
                  new SQLiteParameter("@ArticleContent",articleInfo.ArticleContent),
                  new SQLiteParameter("@CreateTime",articleInfo.CreateTime),
                  new SQLiteParameter("@CreateUser",articleInfo.CreateUser),
                  new SQLiteParameter("@ClickCount",articleInfo.ClickCount),
                  new SQLiteParameter("@Disable",articleInfo.Disable),
                  new SQLiteParameter("@IsNew",articleInfo.IsNew),
                  new SQLiteParameter("@IsTop",articleInfo.IsTop),
            };

            try
            {
              var rows=  DbHelperSQL.ExecuteSql(sb.ToString(), parameters);
              
              if(rows>0)
                return true;
              return false;
              
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                throw;
            }
        }

        public bool ArticleInfo_Remove(ArticleInfo articleInfo)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("delete from [ArticleInfo] ");
            sb.Append(" where ArticleId=@ArticleId");
            SQLiteParameter[] parameters =
            {
                new SQLiteParameter("@ArticleId",articleInfo.ArticleId)
            };

            try
            {
              var rows=DbHelperSQL.ExecuteSql(sb.ToString(), parameters);
              if(rows>0)
                return true;
              return false;
            }
            catch (Exception ex)
            {
                Console.WriteLine("当前时间:{0}，数据编号:{1}删除异常！", DateTime.Now, articleInfo.ArticleId);
                throw;
            }
        }
        
        public bool ArticleInfo_RemoveBatch(string ids)
        {
            if (string.IsNullOrEmpty(ids))
                return true;

            StringBuilder sb = new StringBuilder();

            sb.Append("delete from [ArticleInfo] ");
            sb.Append(" where ArticleId in (" + ids + ")");

            try
            {
                int rows = DbHelperSQL.ExecuteSql(sb.ToString());

                if (rows > 0)
                    return true;
                return false;
            }
            catch (Exception ex)
            {
                throw;
            }
        }
        
        public List<ArticleInfo> ArticleInfo_List(List<Int64> ids)
        {
            var idList = string.Join(",", ids);

            var sql = "select * from [ArticleInfo] where ArticleId in (" + idList + ")";

            var table = DbHelperSQL.Query(sql).Tables[0];

            List<ArticleInfo> list = new List<ArticleInfo>();

            for (int i = table.Rows.Count - 1; i >= 0; i--)
            {
                list.Add(new ArticleInfo(table.Rows[i]));
            }

            return list;
        }
        
        public ArticleInfo ArticleInfo_Single(Int64 id)
        {
            var sql = "select * from [ArticleInfo] where ArticleId=" + id;

            var table = DbHelperSQL.Query(sql).Tables[0];

            return new ArticleInfo(table.Rows[0]);
        }
    }
}
